// 执行命令  ts-node typeInference.ts
// 类型推断
let strin = 'this is string'

// 类型断言
const arrayNumber: number[] = [1, 2, 3, 4];
const greaterThan2: number = arrayNumber.find(num => num > 2) as number; // 提示 ts(2322)
// 尖括号 语法
let someValue: any = "this is a string";
let strLength: number = (<string>someValue).length;
// as 语法
let someValue1: any = "this is a string";
let strLength1: number = (someValue as string).length;
// 以上两种方式虽然没有任何区别，但是尖括号格式会与react中JSX产生语法冲突，因此我们更推荐使用 as 语法。

// 非空断言
let mayNullOrUndefinedOrString: null | undefined | string;
mayNullOrUndefinedOrString!.toString(); // ok
// mayNullOrUndefinedOrString.toString(); // ts(2531)
type NumGenerator = () => number;
function myFunc(numGenerator: NumGenerator | undefined) {
  // Object is possibly 'undefined'.(2532)
  // Cannot invoke an object which is possibly 'undefined'.(2722)
  // const num1 = numGenerator(); // Error
  const num2 = numGenerator!(); //OK
}

// 确定赋值断言
let z!: number;
initialize();

// Variable 'x' is used before being assigned.(2454)
console.log(2 * z); // Error
function initialize() {
  z = 10;
}




console.log('typeInference: ', strin)